<?php
// load the theme options
$options = get_option( 'complete_theme_settings' );
$portfolio_page_name = $options['portfolio_page'];
?>
<?php get_header(); ?>
<div id="main" class="main-with-sidebar clearfix">  
    <div id="post-content">
		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <h1 id="title-with-margin"><?php the_title(); ?></h1>
        <div class="post-entry-meta">
        	<span class="meta-date"><?php the_time('F jS, Y') ?></span> / <?php if($portfolio_page_name != '') { ?><span class="meta-category"><a href="<?php echo get_permalink_by_name($portfolio_page_name); ?>" title="<?php echo $portfolio_page_name ?>"><?php echo $portfolio_page_name?></a></span> / <?php } ?><span class="meta-comments"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
        </div>
        <!-- END post-entry-meta -->
        <?php the_category(' '); ?>
        <?php the_content(' '); ?>	
        <?php endwhile; ?>
        <?php endif; ?>
        <?php
        // check if the related posts are disabled
        if ($options['disable_related_portfolio'] != true) { ?>
        <div id="related-portfolio" class="clearfix">
			<?php 
            $tags = wp_get_post_terms($post->ID, 'portfolio_tags');
            if ($tags) { ?>
            <?php
            // check if user has added a custom title
            if($options['related_portfolio_title'] != '') { ?>
            <h2><?php echo stripslashes($options['related_portfolio_title']);  ?></h2>
            <?php } else { ?>
            <h2><?php _e( 'Related Projects' ); ?></h2>
            <?php } ?>     
				<?php
                $first_tag 	= $tags[0]->term_id;
                $second_tag = $tags[1]->term_id;
                $third_tag 	= $tags[2]->term_id;
                $args = array(
					'post_type' => 'portfolio',
					'posts_per_page' => 4,
					'tax_query' => array(
					'relation' => 'OR',
                array(
					'taxonomy' => 'portfolio_tags',
					'terms' => $second_tag,
					'field' => 'id',
					'operator' => 'IN',
                ),
                array(
					'taxonomy' => 'portfolio_tags',
					'terms' => $first_tag,
					'field' => 'id',
					'operator' => 'IN',
                ),
                array(
					'taxonomy' => 'portfolio_tags',
					'terms' => $third_tag,
					'field' => 'id',
					'operator' => 'IN',
               	)
				));
                $my_query = new WP_Query($args);
                $i=0; //start post count at "0"
                if( $my_query->have_posts() ) {
                while ($my_query->have_posts()) : $my_query->the_post();
                $i++; //add 1 to the total count
                ?>
            <div class="related-portfolio-box opacity <?php if($i==3) { echo 'remove-margin'; } ?>">
                <div class="related-portfolio-box-image">
                    <a href="<?php the_permalink(' ') ;?>" title="<?php the_title(' ') ?>" class="opacity">
                    <?php the_post_thumbnail('related-portfolio-posts'); ?>
                    </a>
                </div>
                <!-- END related-portfolio-box-image -->
            </div>
            <!-- END related-portfolio-box -->
			<?php
            //reset the count to "0" and clear the divs
            if($i===3){ echo '<div class="clear"></div>'; $i=0; }
            endwhile; 
            wp_reset_query(); } } ?>
        </div>
        <!-- END related-portfolio -->
        <?php } ?>
        <?php comments_template(); ?>
    </div>
	<!-- END post-content -->
<?php get_sidebar('portfolio'); ?>
<?php get_footer(' '); ?>